package com.yidu.t299.controller.xp;
import com.yidu.t299.domain.xp.Class;
import com.yidu.t299.domain.xp.ClassManage;
import com.yidu.t299.domain.xp.School;
import com.yidu.t299.domain.xp.StudentManage;
import com.yidu.t299.service.xp.ClassManageBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @descripion: 报班管理控制器
 * @auth: XuPan
 * @date: 2023/3/9 9:19
 * @version: 1.0
 */
@RestController
public class ClassManageController {
    @Autowired
    private ClassManageBiz classManageBiz;//注入数据

    /**
     * 初始化操作，跳转并保存下拉框数据(实现动态下拉框功能)
     */
    @RequestMapping("classManage/init")
    public ModelAndView saveSelect(){
        List<Class> classList = classManageBiz.selectAllGetClass();//得到所有课程名称
        List<School> schoolList = classManageBiz.selectAllGetSchool();//得到所有校区名称
        List<StudentManage> studentManageList = classManageBiz.selectAllGetStudent();//得到所有学员姓名
        ModelAndView mv = new ModelAndView();
        mv.setViewName("classManage");//跳转的页面
        mv.addObject("classList",classList); //保存课程信息
        mv.addObject("schoolList",schoolList); //保存校区信息
        mv.addObject("studentManageList",studentManageList); //保存学员信息
        return mv;
    }

    /**
     * 分页查询显示数据在表格中
     *
     * @param limit 页码
     * @param page  每页的行数
     * @param classManage   接收多条件搜索的表单数据对象
     * @return 映射集合对象
     */
    @GetMapping("classManage/restful_classManage")
    public Map<String, Object> findAll(int limit, int page, ClassManage classManage) {
        // 根据classManage对象得到条件字符串
        String condition = classManageBiz.getCondition(classManage);
        // 按条件统计总记录行数
        int count = classManageBiz.selectCount(condition);
        // 按条件分页查询
        List<ClassManage> classManageList = classManageBiz.selectByPage(limit, page-1, condition);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为报班对象集合的映射
        mapData.put("data", classManageList);
        //把映射集合响应到客户端
        return mapData;
    }

    /**
     * 新增
     * @param classManage
     * @return
     */
    @PostMapping("classManage/restful_classManage")
    public String addEmp(@RequestBody ClassManage classManage) {
        //得到学员姓名和班级名称判断是否为空不为空则进行操作
        if (classManage !=null){
            if (classManage.getClassName()!=null && classManage.getStudentName()!=null){
                //查询出学员姓名和班级名称 存入classManage中
                classManage.setStudentId(classManageBiz.selectByStudentName(classManage.getStudentName()));
                classManage.setClassId(classManageBiz.selectByclassName(classManage.getClassName()));
                //调用业务层实现新增报班，并返回正确与否字符串
                return classManageBiz.insert(classManage) > 0 ? "添加成功" : "添加失败";
            }
        }
        //如果classManage为空直接返回错误信息
        return "添加失败";
    }

    /**
     * 修改
     * @param classManage
     * @return
     */
    @PutMapping("classManage/restful_classManage")
    public String updateEmp(@RequestBody ClassManage classManage) {
        //得到学员姓名和班级名称判断是否为空不为空则进行操作
        if (classManage!=null){
            if (classManage.getClassName()!=null && classManage.getStudentName()!=null){
                //查询出学员姓名和班级名称 存入classManage中
                classManage.setStudentId(classManageBiz.selectByStudentName(classManage.getStudentName()));
                classManage.setClassId(classManageBiz.selectByclassName(classManage.getClassName()));
                //调用业务层实现修改报班，并返回正确与否字符串
                return classManageBiz.updateByPrimaryKey(classManage) > 0 ? "修改成功" : "修改失败";
            }
        }
        //如果classManage为空直接返回错误信息
        return "修改失败";
    }

    /**
     *删除
     * @param logId
     * @return
     */
    @DeleteMapping("classManage/restful_classManage/{claId}")
    public String deleteEmpById(@PathVariable("claId") int logId) {
        //调用业务层实现删除报班，并返回正确与否字符串
        return classManageBiz.deleteByPrimaryKey(logId) > 0 ? "删除成功" : "删除失败";
    }

    /**
     *批量删除
     * @param schoolIdStr
     * @return
     */
    @PutMapping("classManage/batchDel")
    public String deleteEmpByManyId(@RequestParam String schoolIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = schoolIdStr.split(",");
        //循环按报班编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (classManageBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "批量删除失败";
            }
        }
        return "批量删除成功";
    }
}
